﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.AB
{
    /// <summary>電話表 PDF 檔案列表 JSON</summary>

    public class PhoneDocList : IHttpHandler
    {
        private string kPhoneDocListJson = ChrisKit.RootDir + "CCP\\PhoneDoc\\PhoneDocList.json";//ios.ccp.com.tw


        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }
                else if (!SipKit.isCCPUser(userInfo.UserType))
                {
                    throw new Exception("Error_02: Cannot access phone list");//無權限存取電話表
                }


                //【02】回傳 JSON 檔案內容
                //
                resultStr = System.IO.File.ReadAllText(kPhoneDocListJson);
                resp.ContentType = "application/json";
                resp.Charset = null;
                resp.AppendHeader("Cache-Control", "max-age=600");
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = System.Text.Encoding.UTF8.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
